package com.jyh.service.money;


import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.config.quantity.ConstantConfig;
import com.jyh.db.entity.JyhBankList;
import com.jyh.db.entity.JyhBankListExample;
import com.jyh.db.entity.JyhConfig;
import com.jyh.db.entity.JyhConfigExample;
import com.jyh.db.mapper.JyhBankListMapper;
import com.jyh.db.mapper.JyhConfigMapper;
import com.jyh.dbOrder.dao.LoanDao;
import com.jyh.service.ButtonService;
import com.jyh.util.HttpClientUtils;
import com.jyh.util.StringUtils;

@Service
public class WithdrawService extends BaseAction {
	@Autowired
	private LoanDao loanDao; 
	@Autowired
	private JyhConfigMapper configMapper;
	@Autowired
	private JyhBankListMapper bankListMapper;
	@Autowired
	private ButtonService buttonService;
	/**
	 * 获取充值页面数据
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getWithdraw(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("");
		String withdrawState  = request.getParameter("withdrawState");
		String sendSource  = request.getAttribute("sendSource") + "";
		if(StringUtils.isNull(withdrawState)){
			withdrawState  = request.getAttribute("withdrawState") + "";
		}
		String sqls  = "";
		if(StringUtils.isNotNull(withdrawState) && withdrawState.equals("1")){
			// 待处理
			mv = new ModelAndView("money/withdrawPending");
			sqls  = " and withdrawState in (1,4)  order by t1.submitTime desc";
		}else if(StringUtils.isNotNull(withdrawState) && withdrawState.equals("2")){
			// 提现处理中
			mv = new ModelAndView("money/inWithdraw");
			sqls  = " and withdrawState=2  order by t1.processTime desc";
		}else if(StringUtils.isNotNull(withdrawState) && withdrawState.equals("3")){
			// 提现成功
			mv = new ModelAndView("money/withdrawSuccess");
			sqls  = " and withdrawState=3  order by t1.successTime desc";
		}else if(StringUtils.isNotNull(withdrawState) && withdrawState.equals("4")){
			// 提现失败
			mv = new ModelAndView("money/withdrawFail");
			sqls  = " and withdrawState=4  order by t1.failTime desc";
		}
		String userNO  = request.getParameter("userNO");
		String orderNO  = request.getParameter("orderNO");
		String userName  = request.getParameter("userName");
		String channel  = request.getParameter("channel");
		String bank  = request.getParameter("bank");
		String loginAccount  = request.getParameter("loginAccount");
		String bankCardNO  = request.getParameter("bankCardNO");
		String moneyNO  = request.getParameter("moneyNO");
		String submitTime1  = request.getParameter("submitTime1");
		String submitTime2  = request.getParameter("submitTime2");
		
		String page = request.getParameter("currentPage");// 传过来的页数
		int lineSize = 20; // 每页显示记录20条数
		int pageCount = 0; // 总页数
		int count = 0; // 数据总数
		int currentPage = 1; // 当前是第几页,默认是第一页
		if (page == null || "1".equals(page)) {
		} else {
			currentPage = Integer.parseInt(page); // 当前是第几页,默认是第一页
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		String limitSql = " limit " + (currentPage - 1) * lineSize + "," + lineSize;
		String sql1 = "select count(1) as count from jyh_user_withdraw_log t1 "
				+ " left join (select * from jyh_config where groups='channel') t2 on t1.channel=t2.value "
				+ " left join (select * from jyh_config where groups='withdrawState') t3 on t1.withdrawState=t3.value where 1=1 ";
		
		String sql = "select t1.*,t2.valName channelName,t3.valName withdrawStateName from jyh_user_withdraw_log t1 "
				+ " left join (select * from jyh_config where groups='channel') t2 on t1.channel=t2.value "
				+ " left join (select * from jyh_config where groups='withdrawState') t3 on t1.withdrawState=t3.value where 1=1 ";
		if(StringUtils.isNull(sendSource) && !sendSource.equals(1)){
			if (StringUtils.isNotNull(userNO)) {
				sql = sql + " and t1.userNO='" + userNO + "'";
				sql1 = sql1 + " and t1.userNO='" + userNO + "'";
			}
			if (StringUtils.isNotNull(orderNO)) {
				sql = sql + " and t1.orderNO='" + orderNO + "'";
				sql1 = sql1 + " and t1.orderNO='" + orderNO + "'";
			}
			if (StringUtils.isNotNull(userName)) {
				sql = sql + " and t1.userName<='" + userName + "'";
				sql1 = sql1 + " and t1.userName<='" + userName + "'";
			}
			if (StringUtils.isNotNull(channel)) {
				sql = sql + " and t1.channel='" + channel + "'";
				sql1 = sql1 + " and t1.channel='" + channel + "'";
			}
			if (StringUtils.isNotNull(bank)) {
				sql = sql + " and t1.bankName='" + bank + "'";
				sql1 = sql1 + " and t1.bankName='" + bank + "'";
			}
			if (StringUtils.isNotNull(moneyNO)) {
				sql = sql + " and t1.moneyNO='" + moneyNO + "'";
				sql1 = sql1 + " and t1.moneyNO='" + moneyNO + "'";
			}
			if (StringUtils.isNotNull(submitTime1)) {
				sql = sql + " and t1.submitTime>='" + submitTime1 + "'";
				sql1 = sql1 + " and t1.submitTime>='" + submitTime1 + "'";
			}
			if (StringUtils.isNotNull(submitTime2)) {
				sql = sql + " and t1.submitTime<='" + submitTime2 + "'";
				sql1 = sql1 + " and t1.submitTime<='" + submitTime2 + "'";
			}
			if (StringUtils.isNotNull(loginAccount)) {
				sql = sql + " and t1.loginAccount='" + loginAccount + "'";
				sql1 = sql1 + " and t1.loginAccount='" + loginAccount + "'";
			}
			if (StringUtils.isNotNull(bankCardNO)) {
				sql = sql + " and t1.bankCardNO='" + bankCardNO + "'";
				sql1 = sql1 + " and t1.bankCardNO='" + bankCardNO + "'";
			}
		}
		
		sql1 = sql1 + sqls ;
		Map<String, Object> map = loanDao.getMap(sql1);
		count = Integer.parseInt(map.get("count") + "");
		if(count<=lineSize){
			limitSql = " limit 0," + lineSize;
		}
		sql = sql + sqls +  limitSql;
		List<Map<String, Object>> list = loanDao.getList(sql);
		
		int temp = count / lineSize; // 取商
		int tem2 = count % lineSize;// 取余数
		if (tem2 > 0 && tem2 < lineSize && temp > 0) {
			pageCount = count / lineSize + 1; // 总页数
		} else if (tem2 == 0 && temp > 0) {
			pageCount = temp;
		} else {
			pageCount = 1; // 总页数
		}
		if (currentPage >= pageCount) {
			currentPage = pageCount;
		}
		
		// 获取充值渠道
		JyhConfigExample jyhConfigExample = new JyhConfigExample();
		jyhConfigExample.createCriteria().andGroupsEqualTo("channel");
		List<JyhConfig> channelList = configMapper.selectByExample(jyhConfigExample);
//		logger.info("获取充值渠道channelList={}",JSONObject.toJSONString(channelList));
		
		list = (List<Map<String, Object>>) buttonService.getTableButton(29, "orderNO,moneyNO", jsonOutSimplify(list), session);
		mv.addObject("list", list);
		mv.addObject("channelList", channelList);
		mv.addObject("pageCount", pageCount);
		mv.addObject("currentPage", currentPage);
		mv.addObject("count", count);
		//将传递过来的参数回显
		mv.addObject("moneyNO", moneyNO);
		mv.addObject("submitTime1", submitTime1);
		mv.addObject("submitTime2", submitTime2);
		mv.addObject("userNO", userNO);
		mv.addObject("orderNO", orderNO);
		mv.addObject("userName", userName);
		mv.addObject("loginAccount", loginAccount);
		mv.addObject("bankCardNO", bankCardNO);
		mv.addObject("bank", bank);
		mv.addObject("channel", channel);
		return mv;
	}
	
	
	/**
	 * 获取页面筛选--提现银行
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "user")
	public ModelAndView getSelectData(ModelAndView mv) {
		// 获取提现银行
		List<JyhBankList> bankList = bankListMapper.selectByExample(new JyhBankListExample());
		mv.addObject("bankList", bankList);
//		logger.info("获取提现银行bankList={}",JSONObject.toJSONString(bankList));
		return mv;
	}
	
	/**
	 * 放款
	 * @param request
	 * @param session
	 * @return
	 */
	public void getLoan(HttpServletRequest request, HttpSession session) {
		String moneyNO  = request.getParameter("moneyNO");
		String url = ConstantConfig.LOANPAY + "?moneyNO=" + moneyNO;
		HttpClientUtils.doPost(url);
	}
	
	/**
	 * 重新放款
	 * @param request
	 * @param session
	 * @return
	 */
	public void getReLending(HttpServletRequest request, HttpSession session) {
		String orderNO  = request.getParameter("orderNO");
		String url =  ConstantConfig.AGAINLOAN + "?orderNo=" + orderNO;
		HttpClientUtils.doPost(url);
	}
	
}
